<?php
/**
 * 城市提升-地方车展-车展配套费用
 * @author jianhui
 *
 */
class CarMatching extends MY_Controller{
    function CarMatching(){
        parent::MY_Controller();
    }
    /**
     * 添加或修改车展配套费用
     */
    function add(){
        $this->load->model('carMatchingModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId'   => $this->user['userId'],
                'procId'   => (int)$this->input->post('procId'),
                'projectId' => (int)$this->input->post('projectId'),
                'type'      => (int)$this->input->post('type'),
                'descType'  => (int)$this->input->post('descType'),
                'descTypeAlias' => $this->input->post('descTypeAlias'),
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'remarks'   => $this->input->post('remarks'),
                'budgetApply' => (float)$this->input->post('budgetApply'),
                'isSync'        => 0,
            );
            $data['procId'] || showError('请选择主线流程');
            $data['projectId'] || showError('请选择地方车展');
            $data['type'] || showError('类型不能为空');
            $data['descType'] || showError('请选择费用项目描述');
            if ($data['descType'] == -1 && !$data['descTypeAlias']){
                showError('请输入其它项目费用描述');
            }
            $data['startTime'] || showError('请选择活动开始时间');
            $data['endTime'] || showError('请选择活动结束时间');
            $data['budgetApply'] >= 0 || showError('请填写预算费用');
            if ($id){
                $id = $this->carMatchingModel->edit($id, $data);
            }else{
                $data['createTime'] = time();
                $id = $this->carMatchingModel->add($data);
            }
            if ($id){
                showSuccess('保存车展配套成功');
            }else{
                showError('保存车展配套失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            if ($id){
                $matchingData = $this->carMatchingModel->getData(array('id'=>$id, 'storeId'=>$this->user['userId']));
                $matchingData || showError('车展配套数据不存在');
                in_array($matchingData['budgetAuditRs'], array(-1,0,3)) || showError('项目已审核，不允许修改');
                $this->viewData['matchingData'] = $matchingData;
                $procId = $matchingData['procId'];
                $projectId = $matchingData['projectId'];
            }else{
                $procId = (int)$this->input->get('procId');
                $procId || showError('请选择主线流程');
                $projectId = (int)$this->input->get('projectId');
                $projectId || showError('请选择车展ID');
            }
            $this->viewData['procId'] = $procId;
            $this->viewData['projectId'] = $projectId;
            //主线流程数据
            $this->load->model('carProcessModel');
            $procData = $this->carProcessModel->getData($this->viewData['procId']);
            $procData || showError('主线流程数据不存在');
            $this->viewData['procData'] = $procData;
            //是否为特殊补报
            $this->viewData['isBubao'] = $this->auth->isCarBudgetEdit($procData);
            //车展数据
            $this->load->model('carProjectModel');
            $carshowData = $this->carProjectModel->getData($this->viewData['projectId']);
            $carshowData || showError('车展数据不存在');
            $this->viewData['carshowData'] = $carshowData;
            $this->load->view('CarMatching/add', $this->viewData);
        }
    }
    /**
     * 删除下下活动明细
     */
    function del(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的线下活动明细项');
        $this->load->model('carMatchingModel');
        if ($this->carMatchingModel->del(array('id'=>$id,'storeId'=>$this->user['userId']))){
            showSuccess('删除线下活动明细项成功');
        }else{
            showError('删除线下活动明细项失败');
        }
    }
}